/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tencent.tendon.convert.json.listeners;

import com.tencent.tendon.convert.json.*;

/**
 *
 * @author nbzhang
 */
public class JsonBoolArrayListener extends JsonAbstractListener<boolean[]> {

    private static final JsonBoolArrayListener instance = new JsonBoolArrayListener();

    private static final JsonBoolListener listener = JsonBoolListener.getInstance();

    private JsonBoolArrayListener() {
    }

    public static JsonBoolArrayListener getInstance() {
        return instance;
    }

    @Override
    public boolean[] convertFrom(final JsonReader in) {
        int size = 0;
        boolean[] data = new boolean[8];
        in.checkArray();
        while (in.hasNext()) {
            if (size >= data.length) {
                boolean[] newdata = new boolean[data.length + 4];
                System.arraycopy(data, 0, newdata, 0, size);
                data = newdata;
            }
            data[size++] = listener.convertBoolFrom(in);
        }
        boolean[] newdata = new boolean[size];
        System.arraycopy(data, 0, newdata, 0, size);
        return newdata;
    }

    @Override
    public final void convertTo(final JsonWriter out, final boolean[] value) {
        boolean[] values = value;
        if (values == null) {
            out.writeNull();
            return;
        }
        if (values.length == 0) {
            out.write('[', ']');
            return;
        }
        out.write('[');
        boolean first = true;
        for (boolean v : values) {
            if (!first) out.write(',');
            listener.convertBoolTo(out, v);
            if (first) first = false;
        }
        out.write(']');
    }

    @Override
    public Class<boolean[]> getType() {
        return boolean[].class;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }

    public static final class JsonBoolArrayHexListener extends JsonAbstractListener<boolean[]> {

        private static final JsonBoolArrayHexListener instance = new JsonBoolArrayHexListener();

        private JsonBoolArrayHexListener() {
        }

        public static JsonBoolArrayHexListener getInstance() {
            return instance;
        }

        @Override
        public void convertTo(JsonWriter out, boolean[] value) {
            if (value == null) {
                out.writeNull();
                return;
            }
            int size = value.length;
            char[] rs = new char[size + 2];
            rs[0] = '"';
            for (int i = 0; i < size; i++) {
                rs[i + 1] = value[i] ? '1' : '0';
            }
            rs[size] = '"';
            out.write(rs);
        }

        @Override
        public boolean[] convertFrom(final JsonReader in) {
            char[] text = in.readValue();
            if (text == null) return null;
            boolean[] rs = new boolean[text.length];
            for (int i = 0; i < text.length; i++) {
                rs[i] = text[i] == '1';
            }
            return rs;
        }

        @Override
        public Class<boolean[]> getType() {
            return boolean[].class;
        }
    }
}
